//
//  Utils.swift
//  AJiTai
//
//  Created by 周博 on 2020/6/11.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import Foundation
import UIKit
import LocalAuthentication




public func synchronized(_ lock: AnyObject, _ task: () -> Void) {
    objc_sync_enter(lock)
    task()
    objc_sync_exit(lock)
}

/**
* 核心动画状态修改事务
* - parameter disableActions: 是否关闭默认的隐式动画, true表示关闭
*/
public func animationCommit(_ disableActions: Bool = true, _ task: () -> Void) {
    
    // 动画事务, 官方文档上说当commit的时候才会被立即渲染 (实测貌似不commit也会立即渲染)
    // disableActions: 是否去除隐式动画 (uiview的非root layer, 核心动画的参数发生改变的时候都会默认有一个大约0.25秒左右的隐式动画)
    CATransaction.begin()
    CATransaction.setDisableActions(disableActions)
    task()
    CATransaction.commit()
}

/**
 * 默认0.25秒的UIView动画
 */
public func animation(_ animationTime: TimeInterval = 0.25, _ task: @escaping () -> Void) {
    UIView.animate(withDuration: animationTime, animations: task)
}

func getLabelWidth(font: UIFont, height: CGFloat, string: String) -> CGSize {
    let size = CGSize.init(width: CGFloat(MAXFLOAT), height: height)
    let dic = [NSAttributedString.Key.font: font] // swift 4.2
    let strSize = string.boundingRect(with: size, options: [.usesLineFragmentOrigin], attributes: dic, context:nil).size
    return strSize
}

/**
 * 打电话
 */
public func call(with phoneNumber: String) {
    
    let phone = "telprompt://" + phoneNumber
    if UIApplication.shared.canOpenURL(URL(string: phone)!) {
        UIApplication.shared.openURL(URL(string: phone)!)
    }
}



/**
 * 用来切换window跟控制器的过度动画
 */
public func changeRootViewControllerAnimation(controllerCalss: UIViewController.Type, _ animationTime: TimeInterval = 0.5) {
    
    guard let window = UIApplication.shared.keyWindow else {return}
    
    UIView.transition(with: window, duration: animationTime, options: .transitionCrossDissolve) {
        
        let oldState = UIView.areAnimationsEnabled
        UIView.setAnimationsEnabled(false)
        
        let nav = UINavigationController(rootViewController: controllerCalss.getVCInstance())
        window.rootViewController = nav
        UIView.setAnimationsEnabled(oldState)
        
    } completion: { (finish) in
        
    }
}
/**
 * 用来切换window跟控制器的过度动画
 */
public func changeRootViewControllerAnimation(controller: UIViewController, _ animationTime: TimeInterval = 0.5) {
    
    guard let window = UIApplication.shared.keyWindow else {return}
    
    UIView.transition(with: window, duration: animationTime, options: .transitionCrossDissolve) {
        
        let oldState = UIView.areAnimationsEnabled
        UIView.setAnimationsEnabled(false)
        
        let nav = UINavigationController(rootViewController: controller)
        nav.navigationBar.isHidden = true
        
        window.rootViewController = nav
        UIView.setAnimationsEnabled(oldState)
        
    } completion: { (finish) in
        
    }
}


public enum TimeFormat: String {
    case yearMillisecond = "yyyyMMddHHmmssSSS"
    case yearSecond = "yyyyMMddHHmmss"
    case yearMinuteWithSeparator = "yyyy/MM/dd HH:mm"
    case monthSecSingleWithSeparator = "M/d HH:mm:ss"
    case yearSecWithSeparator = "yyyy/MM/dd HH:mm:ss"
    case yearSecWithLine = "yyyy-MM-dd HH:mm:ss"
    case yearMinute = "yyyyMMddHHmm"
    case yearDayWithSeparator = "yyyy/MM/dd"
    case yearDayWithCharacter = "yyyy年MM月dd日"
    case yearMonthWithCharacter = "yyyy年MM月"
    case monthDayWithCharacter = "MM月dd日"
    case yearDay = "yyyyMMdd"
    case yearMonth = "yyyyMM"
    case yearMonthWithSeparator = "yyyy/MM"
    case monthDayWithSeparator = "MM/dd"
    case monthDay = "MMdd"
    case hourMinuteSecond = "HH:mm:ss"
    case yearDayWithHyphen = "yyyy-MM-dd"
    case yearDayWithMinuteHyphen = "yyyy-MM-dd HH:mm"
    case yearMonthWithLine = "yyyy-MM"
    case yearMonthWithSpace = "yyyy年 MM月"
    case yearMonthWithWeek = "yyyy年MM月dd日（E）"
    case yearSingleMonthWithLine = "yyyy-M"
    case yearSingleMonthWithMinuteHyphen = "YYYY/M/d HH:mm"
    case yearDayWithCharacterhourMinute = "yyyy年MM月dd日 HH時mm分"
    case monthDayMinuteWithCharacter = "MM月dd日 HH時mm分"
    case hourMinuteSecondWithCharacter = "HH時mm分ss秒"
    case month = "MM"
    case singleMonth = "M"
    case yearSingleMonthWithSpace = "yyyy年 M月"
}
